/**
* @file imu_icm20602.h in InsCubeBsp
* @author linfe
* @comment
* Create on 2023/1/29 23:33
* @version 1.0
**/

#ifndef INSCUBEBSP_CORE_SRC_DRIVERS_IMU_ICM20602_H_
#define INSCUBEBSP_CORE_SRC_DRIVERS_IMU_ICM20602_H_
#include "../Bsp/bsp.h"

typedef struct {
  float acce_scale;
  float gyro_scale;
  char name[16];
  uint32_t id;
  float acce_full_scale;
  float gyro_full_scale;
  uint32_t max_sample_rate;
  float delta_t;
} icm_attribute_t;
/*NOTE: If both GYRO_FIFO_EN And ACCEL_FIFO_EN are 1, write
 * ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H, ACCEL_YOUT_L, ACCEL_ZOUT_H, ACCEL_ZOUT_L,
TEMP_OUT_H, TEMP_OUT_L, GYRO_XOUT_H, GYRO_XOUT_L, GYRO_YOUT_H, GYRO_YOUT_L, GYRO_ZOUT_H, and GYRO_ZOUT_L*/
typedef struct {
  int16_t acce[3];
  int16_t temp;
  int16_t gyro[3];
} imu_raw_icm_t;
typedef enum {
  ICM20602AcceFs_2G = 0,
  ICM20602AcceFs_4G = 1,
  ICM20602AcceFs_8G = 2,
  ICM20602AcceFs_16G = 3,
} icm20602_acce_full_scale_t;
typedef enum {
  ICM20602GyroFs_250DEG = 0,
  ICM20602GyroFs_500DEG,
  ICM20602GyroFs_1000DEG,
  ICM20602GyroFs_2000DEG,
} icm20602_gyro_full_scale_t;
typedef enum {
  ICM20602SampleRate_10Hz = 0,
  ICM20602SampleRate_50Hz ,
  ICM20602SampleRate_100Hz,
  ICM20602SampleRate_200Hz,
  ICM20602SampleRate_500Hz,
  ICM20602SampleRate_1000Hz,
} icm20602_sample_rate_t;

typedef struct {
  struct {
    icm20602_acce_full_scale_t acce_full_scale;
    icm20602_gyro_full_scale_t gyro_full_scale;
    icm20602_sample_rate_t sample_rate;
  } config;
  bsp_device_t *dev;                             /* bsp_device*/
  void (*delay_ms)(uint32_t ms);                /* 延时函数,单位ms*/
  int (*trace)(const char *fmt, ...);           /* 打印函数*/
  bsp_gpio_pin data_ready_pin;                  /* data ready 中断引脚*/
  icm_attribute_t *attrib;                      /* pointer to imu attribute struct*/
} icm2060x_handle_t;

typedef enum {
  icm2060x_OK = 0,
  icm2060x_wrong_id,
  icm2060x_reset_failed,
  icm2060x_spi_write_error,
  icm2060x_spi_read_error
} icm2060x_error_t;

icm2060x_error_t icm2060x_init(icm2060x_handle_t *handle);
icm2060x_error_t icm2060x_read(icm2060x_handle_t *handle, imu_raw_icm_t *icm);
#endif //INSCUBEBSP_CORE_SRC_DRIVERS_IMU_ICM20602_H_
